<template>
  <div>
    <div class="header">
      <img :src="userInfo.avatarUrl?userInfo.avatarUrl:'/static/images/index/touxiang.png'" alt="">
      <button class="btn" @tap="handleTap" >{{userInfo.nickName?userInfo.nickName:'登录'}}</button>
    </div>
    <div id="message">
      <button @click="toMyCenter">我的信息</button>
      <button @click="toMyActivity">参加的活动</button>
      <button v-if="user.isAdmin==1" @click="toMyCreate">创建的活动</button>
      <button v-if="user.isAdmin==1" @click="toMyCreate">创建的公告</button>
    </div>
  </div>
</template>


<script>
  import request from '../../utils/request'
  export default{
    data(){
      return {
        userInfo:{},
        user:''
      }
    },
    methods: {
      handleTap(){
        wx.getUserProfile({
          desc: '用于完善资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: (res) => {
            this.userInfo=res.userInfo;
            wx.setStorageSync('userInfo',this.userInfo)  
            wx.login({ 
              success: async (res) => {
              //let code = res.code  //有效时长5分钟
              let data ={
                code:res.code,
                img:this.userInfo.avatarUrl,
                wxname:this.userInfo.nickName
              }
              console.log(data);
              let token = await request('/user/getOpenId',{data},'GET');
              //将自定义登陆状态缓存到storage中 
              wx.setStorageSync('token',token)
              console.log(token)
              }             
            })   
            //console.log(res.userInfo.avatarUrl);
          },
          fail: (res) => {
            this.userInfo={};
            console.log(res);
          },
        })
      },
      toMyCenter(){
        wx.navigateTo({
          url:'/pages/myCenter/main'
        })
      },
      toMyActivity(){
        wx.navigateTo({
          url:'/pages/myActivity/main'
        })
      },
      toMyCreate(){
         wx.navigateTo({
          url:'/pages/myCreate/main'
        })
      }
    },
    mounted() {
      this.userInfo = wx.getStorageSync('userInfo') 
      this.user = wx.getStorageSync('user') 
      console.log(this.user.isAdmin)
    },
  }
</script>

<style scoped>
.header{
  width:100%;
  height:100px;
  background: red;
}
.header img{
  width:70px;
  height:70px;
  margin-top: 20px;
  margin-left: 20px;
  border-radius: 35px;
}

.header .btn{
  width:100px;
  display: inline-block;
  margin-left:20px;
  margin-bottom: 10px;
  opacity: .5;
  line-height: 40px;
  max-width: 100px;
}


</style>
 